<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>强大的原型和原型链</title>
    <script type="text/javascript">
        /*
         * 代码片段1
         * 原型使用方式1
         */
        //定义对象
        var Calculator = function (decimalDigits, tax) {
            this.decimalDigits = decimalDigits;
            this.tax = tax;
        };
        Calculator.prototype = {
            add: function (x, y) {
                return x + y;
            },
            subtratct: function (x, y) {
                return x - y;
            }
        };

        //调用
        alert((new Calculator().add(3, 2)));

        /*
         * 代码片段2
         * 原型使用方式2
         * 在赋值原型prototype的时候使用function立即执行的表达式来赋值
         */
        //定义对象
        var Calculator2 = function (decimalDigits, tax) {
            this.decimalDigits = 2;
            this.tax = 5;
        };
        Calculator2.prototype = function () {
            add = function (x, y) {
                return x + y;
            },
                    subtract = function (x, y) {
                        return x - y;
                    },
                    chengfa = function (x, y) {
                        return x * y;
                    }
            //此处，只将加法跟减法暴露出简单的使用名称，以达到public、private的效果
            //chengfa() 未暴露在外，所以以下不能调用
            return {
                add: add,
                subtract: subtract
            }
        }();

        //调用
        alert((new Calculator2()).add(3, 2));
        alert((new Calculator2()).decimalDigits);   //可以访问Calculator2下的属性

        /*
         * 代码片段3
         * 【是否能够】 访问基础构造函数里声明的属性值
         */
        //定义 基础原型
        var BaseCalculator = function () {
            this.decimalDigits = 2;
        };
        //通过【原型】属性设置2个function
        BaseCalculator.prototype = {
            add: function (x, y) {
                return x + y;
            },
            suntract: function (x, y) {
                return x - y;
            }
        };

        //直接new一个新的类似【对象】，能访问基础原型内设置的属性
        var calcultor = new BaseCalculator();
        alert(calcultor.decimalDigits);

        //方法2
        //扩展【基础原型】,添加新的方法属性。
        var UseCalcultor = function () {
            //为每个实例都声明一个税收数字
            this.tax = 5.5;
        };
        //由于它的原型是BaseCalculator的一个实例，所以不管你创建多少个Calculator对象实例，他们的原型指向的都是同一个实例。
        UseCalcultor.prototype = new BaseCalculator();
        var calc = new UseCalcultor();
        alert(calc.add(15, 20));
        alert(calc.decimalDigits);

        //方法3，不能访问
        var UseCalcultor2 = function () {
            this.tax = 2.2;
        };
        //通过将BaseCalculator的原型赋给Calculator的原型，这样你在Calculator的实例上就访问不到那个decimalDigits值了
        UseCalcultor2.prototype = BaseCalculator.prototype;
        var calc2 = new UseCalcultor2();
        alert(calc2.add(1, 2));
        alert(calc2.decimalDigits);     //undefinedkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk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